/**
 * 提供亂碼字串來源
 */
package kg.tool.string;

import java.util.Random;

/**
 * 處理傳回亂數字串用
 * @author Kahiro
 *
 */
public class RandomString {
	private static final int ALPHABETIC_NUMERIC_SIZE = 62;
	private static final int ALPHABETIC_SIZE = 26;
	private static final int NUMERIC_SIZE = 10;
	
	private static final int NUMERIC_START_INDEX = 48;
	private static final int NUMERIC_END_INDEX = 57;
	private static final int ALPHABETIC_UPPERCASE_START_INDEX = 65;
	private static final int ALPHABETIC_UPPERCASE_END_INDEX = 90;
	private static final int ALPHABETIC_LOWERCASE_START_INDEX = 97;
	private static final int ALPHABETIC_LOWERCASE_END_INDEX = 122;
	 
		
	private static char[] an = new char[ALPHABETIC_NUMERIC_SIZE];
	
	private static RandomString instance;
	
	static{
		for(int i = 0,c = NUMERIC_START_INDEX; c <= ALPHABETIC_LOWERCASE_END_INDEX; i++,c++){
			if(c < NUMERIC_END_INDEX){
				an[i] = (char)c;
			}else if(c == NUMERIC_END_INDEX){
				an[i] = (char)c;
				c = ALPHABETIC_UPPERCASE_START_INDEX - 1;
			}else if(c < ALPHABETIC_UPPERCASE_END_INDEX){
				an[i] = (char)c;
			}else if(c == ALPHABETIC_UPPERCASE_END_INDEX){
				an[i] = (char)c;
				c = ALPHABETIC_LOWERCASE_START_INDEX - 1;
			}else{
				an[i] = (char)c;
			}
		}
	}
	
	/**
	 * 不允許由外部產生實例
	 */
	private RandomString(){};
	
	/**
	 * get RandomString instance
	 * @return
	 */
	public static RandomString getInstance(){
		if(instance == null){
			instance = new RandomString();
		}
		return instance;
	}
	
	/**
	 * 取得隨機字母字串(區分大小寫)
	 * @param length
	 * @return
	 */
	public final String getRandomAlphabetic(int length){
		StringBuffer sb = new StringBuffer(length);
		Random ran = new Random();
		int range = ALPHABETIC_SIZE * 2;
		for(int i = 0; i < length; i++){
			sb.append(an[ NUMERIC_SIZE + ran.nextInt(range)]);
		}
		return sb.toString();
	}
	
	/**
	 * 取得隨機大寫字母字串
	 * @param length
	 * @return
	 */
	public final String getRandomAlphabeticUpperCase(int length){
		StringBuffer sb = new StringBuffer(length);
		Random ran = new Random();
		int range = ALPHABETIC_SIZE;
		for(int i = 0; i < length; i++){
			sb.append(an[ NUMERIC_SIZE + ran.nextInt(range)]);
		}
		return sb.toString();
	}
	
	/**
	 * 取得隨機小寫字母字串
	 * @param length
	 * @return
	 */
	public final String getRandomAlphabeticLowerCase(int length){
		return getRandomAlphabeticUpperCase(length).toLowerCase();
	}
	
	/**
	 * 取得随機數字字串
	 * @param length
	 * @return
	 */
	public final String getRandomNumeric(int length){
		StringBuffer sb = new StringBuffer(length);
		Random ran = new Random();
		int range = NUMERIC_SIZE;
		for(int i = 0; i < length; i++){
			sb.append(an[ran.nextInt(range)]);
		}
		return sb.toString();
	}
	
	/**
	 * 取得隨機文數字字串(區分大小寫)
	 * @param length
	 * @return
	 */
	public final String getRandomAlphaNumeric(int length){
		StringBuffer sb = new StringBuffer(length);
		Random ran = new Random();
		int range = ALPHABETIC_NUMERIC_SIZE;
		for(int i = 0; i < length; i++){
			sb.append(an[ran.nextInt(range)]);
		}
		return sb.toString();
	}
	
	/**
	 * 取得隨機文數字字串(大寫)
	 * @param length
	 * @return
	 */
	public final String getRandomAlphaNumericUpperCase(int length){
		StringBuffer sb = new StringBuffer(length);
		Random ran = new Random();
		int range = NUMERIC_SIZE + ALPHABETIC_SIZE;
		for(int i = 0; i < length; i++){
			sb.append(an[ran.nextInt(range)]);
		}
		return sb.toString();
	}
	
	/**
	 * 取得隨機文數字字串(小寫)
	 * @param length
	 * @return
	 */
	public final String getRandomAlphaNumericLowerCase(int length){
		return getRandomAlphaNumericUpperCase(length).toLowerCase();
	}
	
	public static void main(String[] args){
		RandomString instance = RandomString.getInstance();
		System.out.println("字母不分大小寫:" + instance.getRandomAlphabetic(10));
		System.out.println("字母大寫：" + instance.getRandomAlphabeticUpperCase(10));
		System.out.println("字母小寫:" + instance.getRandomAlphabeticLowerCase(10));
		System.out.println("數字:" + instance.getRandomNumeric(10));
		System.out.println("文數字:" + instance.getRandomAlphaNumeric(10));
		System.out.println("文數字大寫:" + instance.getRandomAlphaNumericUpperCase(10));
		System.out.println("文數字小寫:" + instance.getRandomAlphaNumericLowerCase(10));
	}
}
